﻿using BySoft.Server.Business;
using BySoft.Server.Business.UserCode;
using BySoft.Server.Common;
using BySoft.Server.Common.Entity;
using BySoft.Web.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BySoft.Web.Site.ComPage
{
    public partial class SetHalfCheck : BasePopPage
    {
        #region 变量定义

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                hidHalfTransferID.Value = UIHelper.GetQueryStringValue("id");
                InitData(hidHalfTransferID.Value);
            }
        }

        void InitData(string strCode)
        {


            BsHalftransferEntity entity = BsHalftransfer.GetBsHalftransferByID(strCode);
            lblActulProduceNum.Text = entity.ActulProduceNum.ToString();

            lblQualityNum.Text = lblActulProduceNum.Text;
            plUnQualifiedNum.Visible = false;

        }

        protected void btnOk_Click(object sender, EventArgs e)
        {
            try
            {
                if (!ValidateData())
                {
                    return;
                }

                string strBillID = CommonMethod.GetString(hidHalfTransferID.Value.Trim());

                BsHalftransferEntity entity = BsHalftransfer.GetBsHalftransferByID(strBillID);

                string strCheckStatus = CommonMethod.GetString(ddlCheckResult.SelectedValue.Trim());
                entity.CheckResult = strCheckStatus;
                if (strCheckStatus == "1")
                {
                    entity.UnqualifyNum = 0;

                    entity.UnqualifyReason = "";

                }
                else if (strCheckStatus == "5")
                {
                    entity.UnqualifyNum = CommonMethod.ConvertToDecimal(txtUnqualifyNum.Text.Trim());

                    entity.UnqualifyReason = CommonMethod.GetString(txtUnqualifyReason.Text.Trim());

                }
                entity.CheckUserID = CurrentUser.UserID;

                BsHalfTransferBusiness.SaveOrUpdate(entity, "Update");


                string sReturn = "success";
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>setCookie(dialogReturn_key,'" + sReturn + "',1);CloseTopDialog();</script>");
            }
            catch (Exception ex)
            {
                UIHelper.AlertMessage(this.Page, ex.Message.ToString());
            }
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            try
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>CloseTopDialog();</script>");
            }
            catch (Exception ex)
            {
                UIHelper.Alert(this.Page, ex.ToString().Trim());
            }

        }

        private bool ValidateData()
        {
            decimal dRecieve = CommonMethod.ConvertToDecimal(lblActulProduceNum.Text);
            decimal dUnQualifiedNum = CommonMethod.ConvertToDecimal(txtUnqualifyNum.Text);
            string strMsg = "";
            if (string.IsNullOrEmpty(ddlCheckResult.SelectedValue))
            {
                UIHelper.AlertMessage(this.Page, "请选择检验结果！");
                return false;
            }

            if (ddlCheckResult.SelectedValue == "5")
            {
                lblQualityNum.Text = (dRecieve - dUnQualifiedNum).ToString();
                if (dUnQualifiedNum <= 0)
                {
                    UIHelper.AlertMessage(this.Page, "请填写不合格数量！");
                    return false;
                }

                if (string.IsNullOrEmpty(txtUnqualifyReason.Text))
                {
                    UIHelper.AlertMessage(this.Page, "请填写不合格原因！");
                    return false;
                }
            }

            return true;
        }

        protected void ddlCheckResult_SelectedIndexChanged(object sender, EventArgs e)
        {

            try
            {
                plUnQualifiedNum.Visible = false;
                lblQualityNum.Text = lblActulProduceNum.Text;
                txtUnqualifyNum.Text = "";

                txtUnqualifyReason.Text = "";
                if (ddlCheckResult.SelectedValue == "3")
                {
                    plUnQualifiedNum.Visible = false;
                }
                else if (ddlCheckResult.SelectedValue == "5")
                {

                    plUnQualifiedNum.Visible = true;
                }
            }
            catch (Exception ex)
            {
                UIHelper.AlertMessage(this.Page, ex.ToString());
            }
        }
    }
}